package com.demo.apparel.service.web.Impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.apparel.dto.web.AgreementPageQueryDTO;
import com.demo.apparel.entity.AdminInfo;
import com.demo.apparel.entity.Agreement;
import com.demo.apparel.mapper.web.AdminInfoMapper;
import com.demo.apparel.mapper.web.WebAgreementMapper;
import com.demo.apparel.service.web.WebAgreementService;
import com.demo.apparel.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class WebAgreementServiceImpl implements WebAgreementService {

    @Autowired
    private WebAgreementMapper webAgreementMapper;

    @Autowired
    private AdminInfoMapper adminInfoMapper;


    @Override
    public Agreement saveAgreement(Agreement agreement) {
        if (agreement.getId() == null || agreement.getId().isEmpty()) {
            agreement.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        webAgreementMapper.insert(agreement);
        return agreement;
    }



    @Override
    public List<Agreement> getAllAgreements() {
        return webAgreementMapper.selectList(null);  // 查询所有记录，传 null 为不加任何条件
    }


    @Override
    public Agreement getAgreementById(String id) {
        return webAgreementMapper.selectById(id);
    }


    @Override
    public PageVO<Agreement> pageQueryAgreements(AgreementPageQueryDTO dto) {
        Page<Agreement> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<Agreement> wrapper = new LambdaQueryWrapper<>();

        if (dto.getIsActive() != null) {
            wrapper.eq(Agreement::getIsActive, dto.getIsActive());
        }
        if (StringUtils.hasText(dto.getVersion())) {
            wrapper.eq(Agreement::getVersion, dto.getVersion());
        }
        if (dto.getCreatedTimeStart() != null && dto.getCreatedTimeEnd() != null) {
            wrapper.between(Agreement::getCreatedTime, dto.getCreatedTimeStart(), dto.getCreatedTimeEnd());
        }

        // nickname模糊查询 -> 查 admin_info 表对应 admin_id
        if (StringUtils.hasText(dto.getNickname())) {
            List<String> adminIds = adminInfoMapper.selectList(
                    new LambdaQueryWrapper<AdminInfo>().like(AdminInfo::getNickname, dto.getNickname())
            ).stream().map(AdminInfo::getAdminId).collect(Collectors.toList());

            if (adminIds.isEmpty()) {
                return new PageVO<>(List.of(), 0, dto.getPageNum(), dto.getPageSize());
            }
            wrapper.in(Agreement::getAdminId, adminIds);
        }

        wrapper.orderByDesc(Agreement::getCreatedTime);
        webAgreementMapper.selectPage(page, wrapper);

        return new PageVO<>(page.getRecords(), page.getTotal(), dto.getPageNum(), dto.getPageSize());
    }
}
